//
//  LCRadiodetailViewController.m
//  YinYueTai
//
//  Created by CongCong on 15-7-18.
//  Copyright (c) 2015年 CongCong. All rights reserved.
//

#import "LCRadiodetailViewController.h"

@interface LCRadiodetailViewController ()
@property (nonatomic) LCRadioDetailModel *detailModel;
@end

@implementation LCRadiodetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatCollection];
    [self registerNib];
    [self creatRefresh];
    [self fetchWebDataWith:kRadioDetailHeader body:[NSString stringWithFormat:kRadioDetailBody,self.radioid,self.page]];
}
- (void)registerNib
{
    
    self.flowLayout.minimumLineSpacing = 15;
    self.flowLayout.minimumInteritemSpacing = 10;
    [self.collectionView registerNib:[UINib nibWithNibName:@"LCRadioListCell" bundle:nil] forCellWithReuseIdentifier:@"LCRadioListCell"];
    
}
- (void)creatRefresh
{
    __weak typeof(self) weakSelf = self;
    [self.collectionView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        weakSelf.page = 0;
        [weakSelf fetchWebDataWith:kRadioDetailHeaderMore body:[NSString stringWithFormat:kRadioDetailBody,weakSelf.radioid,weakSelf.page]];
    }];
    [self.collectionView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        weakSelf.page+=10;
        if (weakSelf.page>=weakSelf.detailModel.total.integerValue) {
            [MMProgressHUD show];
            [MMProgressHUD  dismissWithSuccess:@"到底了..."];
            [weakSelf.collectionView footerEndRefreshing];
            return ;
        }
        [weakSelf fetchWebDataWith:kRadioDetailHeaderMore body:[NSString stringWithFormat:kRadioDetailBody,weakSelf.radioid,weakSelf.page]];
    }];
}
- (void)initJosnModelWith:(NSDictionary *)dataDict
{
    if (self.page) {
        NSDictionary *dict = dataDict[@"data"];
        LCRadioDetailModel *model = [[LCRadioDetailModel alloc]initWithDictionary:dict error:nil];
        [self.detailModel.list addObjectsFromArray:model.list];
    }
    else
    {
        NSDictionary *dict = dataDict[@"data"];
        self.detailModel = [[LCRadioDetailModel alloc]initWithDictionary:dict error:nil];
    }
    [self.collectionView reloadData];
}

#pragma mark - <UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.detailModel.list.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCRadioListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCRadioListCell" forIndexPath:indexPath];
    LCRdDetailListModel *model = self.detailModel.list[indexPath.row];
    [cell showModel:model];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(kScreenSize.width-kScreenSize.height/662*20, 50);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //点击事件
    LCRdPlayInfo *playModel = [self.detailModel.list[indexPath.row] playInfo];
    LCRdListenViewController *listen = [LCRdListenViewController shareListen];
    listen.playInfoModel = playModel;
    
    listen.musicArray = [self.detailModel.list mutableCopy];
    listen.index = indexPath.row;
    
    [self.navigationController pushViewController:listen animated:YES];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *header;
     UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height/662*200)];
    image.tag = 200;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, kScreenSize.height/662*200, kScreenSize.width, kScreenSize.height/662*80)];
    label.numberOfLines = 0;
    label.tag = 201;
    LCRdRadioInfo *model = self.detailModel.radioInfo;
    if([kind isEqual:UICollectionElementKindSectionHeader])
    {
        
        header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
        UILabel *oldLabel = (UILabel*)[header viewWithTag:201];
        [oldLabel removeFromSuperview];
        UIImageView *oldImage = (UIImageView*)[header viewWithTag:200];
        [oldImage removeFromSuperview];
        if (model==nil) {
            return header;
        }
        
    }
    [image sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:[UIImage imageNamed:@"hoder"]];
    NSString *title = [NSString stringWithFormat:@"%@\n\n播放次数：%@",model.desc,model.musicvisitnum];
    label.text = title;
    [header addSubview:image];
    [header addSubview:label];
    
    return header;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(kScreenSize.width, kScreenSize.height/662*280);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
